<?php
class clientsclass {

	var $client_info;
	
	function clientsclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->get_curent_client_info();
	}

	function get_curent_client_info(){
		if ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)) {
			$client = array();
			$id = $_SESSION['client_login_id'];
			if (isset($id) && (strlen($id) > 0)){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client` WHERE `id`=".$id, __LINE__, __FILE__);		
				if (mysql_num_rows($sql) == 1){
					$info = mysql_fetch_array($sql, MYSQL_ASSOC);
					$client['main'] = $info;
					$client['data']['id_email']['value'] = $info['email'];
					$client['data']['id_email']['type'] = 'id_email';
					$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent`=".$info['id']." ORDER BY `pos` ASC", __LINE__, __FILE__);		
					if (mysql_num_rows($sql2) > 0){
						while ($row = mysql_fetch_array($sql2, MYSQL_ASSOC)) {
							$client['data'][$row['name']]['value'] = stripslashes($row['value']);
							$client['data'][$row['name']]['type'] = $row['type'];
						}
					}
				} 
			}
			$client_info = $client;
			$groups = array();
			$sql = $this->mysqlclass->mysql_my_query("SELECT `group_id` FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `client_id`=".$id, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))	$groups[$row['group_id']] = $row['group_id'];
			$client_info['groups'] = $groups;
			$this->client_info = $client_info;
		}
	}
	
	function get_clients_by_search($search_query){
		$result = array();
		if (isset($search_query) && (strlen($search_query) > 0)){
			$search_query = trim($search_query);
			$id_array = array();
			$query = "SELECT `parent` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `value` LIKE '%".$search_query."%'";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $id_array[$row['parent']] = $row['parent'];
			$query = "SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `email` LIKE '%".$search_query."%'";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $id_array[$row['id']] = $row['id'];
			foreach($id_array as $id) {
				$result[$id] = $this->get_clients_info_by_id($id);
				$result[$id]['groups'] = $this->get_one_client_groups($id);
			}
		}
		return $result;
	}

	function group_get_info($id=0){
		if (intval($id) == 0) return 0;
		$result = array();
		$id = intval($id);
		$query = "SELECT * FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `id`=".$id;
		$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) $row = mysql_fetch_array($sql, MYSQL_ASSOC);
		else return 0;
		if (!isset($row['parent']) || ($row['parent'] == 0)) return $row;
		$query = "SELECT * FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `id`=".$row['parent'];
		$sql2 = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
		$rows2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
		$result = $row;
		$result['parent'] = $rows2;
		return $result;
	}
	
	function group_get($id=0){
		if (intval($id) == 0) $id = 0;
		$result = array();
		$id = intval($id);
		$query = "SELECT * FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `parent`=".$id;
		$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
		while ($rows = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$rows['id']] = $rows;
			$result2 = array();
			$query = "SELECT * FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `parent`=".$rows['id'];
			$sql2 = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($rows2 = mysql_fetch_array($sql2, MYSQL_ASSOC)){
				$result[$rows2['id']] = $rows2;
				$result[$rows2['id']]['parent'] = $rows;
			}
		}
		return $result;
	}

	function group_add($parent, $name){
		if (isset($parent) && (strlen($parent) > 0)){
			$parent = intval($parent);
			$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) as `pos` FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `parent`=".$parent, __LINE__, __FILE__);
			$num = mysql_num_rows($sql);
		} else $num = 0;
		if ($num == 0) $pos = 0;
		else {
			$rows = mysql_fetch_array($sql, MYSQL_ASSOC);
			$pos = $rows['pos'] + 1;
		}
		$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_groups` (`id`, `parent`, `name`, `pos`) VALUES ('', '".$parent."', '".$name."', ".$pos.")", __LINE__, __FILE__);
	}
		
	function group_del($arr){
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM`".$this->setupdata['table_prefix']."client_groups` WHERE `id` = ".$elem." OR `parent`=".$elem, __LINE__, __FILE__);		
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM`".$this->setupdata['table_prefix']."client_depend` WHERE `group_id` = ".$elem, __LINE__, __FILE__);		
			}
		}
	}
	
	function group_mod($parent, $name){
		if (isset($parent) && (strlen($parent) > 0)){
			$parent = intval($parent);
			if (!isset($name) && (strlen($name) == 0)) return 5;
			$name = trim($name);
			$name = addslashes($name);
			$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_groups` SET `name`='".$name."' WHERE `id` = ".$parent, __LINE__, __FILE__);
		}
	}
	
	function mod_user_registation_data($id, $array){
		if (isset($id) && (strlen($id) > 0)){
			$id = intval($id);
			if (isset($array['id_email']) && (strlen($array['id_email']) > 0)){
				if (!(eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $array['id_email']))) return 13;
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `email`='".$array['id_email']."'", __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0) {
					$row = mysql_fetch_array($sql, MYSQL_ASSOC);
					if ($row['id'] != $id) return 0;
				}
			} 
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `id`=".$id, __LINE__, __FILE__);
			if (mysql_num_rows($sql) == 0) return 0;
			foreach($array as $key=>$val){
				if ($key == "id_email") {
					if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $val)) {
						$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client` SET `email`='".$val."' WHERE `id` = ".$id, __LINE__, __FILE__);
					}
				} elseif (($key == "id_pass") || ($key == "id_pass2")) {
					if (($key == "id_pass") && (isset($array['id_pass2'])) && ($val==$array['id_pass2'])){
						if (strlen($val) > 0){
							$val = md5($val);
							$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name`='id_pass' AND `parent`=".$id, __LINE__, __FILE__);
							if (mysql_num_rows($sql) > 0){
								if( strlen($array['id_pass2']) < 50 ) { //added to prevent psw change with no edit
									$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='".$val."' WHERE `parent` = ".$id." AND `name`='id_pass'", __LINE__, __FILE__);
								}
							} else {
								$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$id.", 'id_pass', 'id_pass', '".$val."', 2)", __LINE__, __FILE__);						
							}
						}
					}
				} elseif ($key == "id_login"){
					if ((strlen($val) > 0) && (!ereg("[^a-zA-Z0-9_-]", $val))){
						$sql = $this->mysqlclass->mysql_my_query("SELECT `parent` FROM `".$this->setupdata['table_prefix']."client_data` WHERE (`parent`=".$id." AND `name`='id_login') OR (`name`='id_login' AND `value`='".$val."')", __LINE__, __FILE__);
						if (mysql_num_rows($sql) > 0){
							$row = mysql_fetch_array($sql, MYSQL_ASSOC);
							if ($row['parent'] == $id) $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='".$val."' WHERE `parent` = ".$id." AND `name`='id_login'", __LINE__, __FILE__);
						} else {
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$id.", 'id_login', 'id_login', '".$val."', 1)", __LINE__, __FILE__);
						}
					}
				} else {
					if (is_array($val) && !isset($val['error'])){
						$val['value'] = addslashes($val['value']);
						$val['type'] = addslashes(strip_tags($val['type']));
						$key = base64_decode($key);
						$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name`='".$key."' AND `parent`=".$id, __LINE__, __FILE__);
						if (mysql_num_rows($sql) > 0){
							$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='".$val['value']."' WHERE `parent` = ".$id." AND `name`='".$key."'", __LINE__, __FILE__);
						} else {
							$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) as `pos` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent`=".$id, __LINE__, __FILE__);
							if (mysql_num_rows($sql) == 0) $pos = 0;
							else {
								$rows = mysql_fetch_array($sql, MYSQL_ASSOC);
								$pos = $rows['pos'] + 1;
							}
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$id.", '".$key."', '".$val['type']."', '".$val['value']."', ".$pos.")", __LINE__, __FILE__);
						}
					} elseif (is_array($val) && isset($val['error']) && isset($val['tmp_name']) && isset($val['size'])){
						$key = base64_decode($key);
						
						global $err, $imageclass;
						$file = $val;
						$file_name1=$val['name'];
						$v = $file['tmp_name'];
						if ($v != ""){
							$val = "";
							if (isset($this->setupdata['file_size']) && ($file['size']/1024 > $this->setupdata['file_size'])) $err->AddError(28);
							else {
								$file_name = "";
								$today = uniqid("");
								$imagedata = getimagesize($v);
								if (!isset($imagedata['mime'])){
									if ($imagedata[2] == 1) $imagedata['mime'] = 'image/gif';
									elseif ($imagedata[2] == 2) $imagedata['mime'] = 'image/jpeg';
								}
								if ($imagedata === FALSE) $err->AddError(7);
								else {
									if ($imagedata['mime'] == 'image/jpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpg";
									elseif ($imagedata['mime'] == 'image/pjpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpeg";
									elseif ($imagedata['mime'] == 'image/gif') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".gif";
									else $err->AddError(7);
									if (strlen($file_name) > 3){
										$file_to = $file_name;
										if (strlen($file_name1) <= 0) $file_name1=$_FILES['img_file1']['name'];
										if (!(file_exists($this->setupdata['binary_path']."/".$parent))){
											if ((mkdir($this->setupdata['binary_path']."/".$parent, 0777)) && (copy($file['tmp_name'], $this->setupdata['binary_path']."/".$parent."/".$file_to))){
												if (file_exists($this->setupdata['binary_path']."/".$parent."/".$file_to)){
													chmod($this->setupdata['binary_path']."/".$parent."/".$file_to, 0666);
													$val = $imageclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
												}
											}
										} else {
											if (copy($file['tmp_name'], $this->setupdata['binary_path']."/".$parent."/".$file_to)){
												if (file_exists($this->setupdata['binary_path']."/".$parent."/".$file_to)){
													chmod($this->setupdata['binary_path']."/".$parent."/".$file_to, 0666);
													$val = $imageclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
												}
											}
										}
									}
								}
							}
						}

						
						$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name`='".$key."' AND `parent`=".$id, __LINE__, __FILE__);
						if (mysql_num_rows($sql) > 0){
							$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='".$val."' WHERE `parent` = ".$id." AND `name`='".$key."'", __LINE__, __FILE__);
						} else {
							$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) as `pos` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent`=".$id, __LINE__, __FILE__);
							if (mysql_num_rows($sql) == 0) $pos = 0;
							else {
								$rows = mysql_fetch_array($sql, MYSQL_ASSOC);
								$pos = $rows['pos'] + 1;
							}
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$id.", '".$key."', 'file', '".$val."', ".$pos.")", __LINE__, __FILE__);
						}
					} else {
						$val = addslashes($val);
						$key = base64_decode($key);
						$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name`='".$key."' AND `parent`=".$id, __LINE__, __FILE__);
						if (mysql_num_rows($sql) > 0){
							$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='".$val."' WHERE `parent` = ".$id." AND `name`='".$key."'", __LINE__, __FILE__);
						} else {
							$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) as `pos` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent`=".$id, __LINE__, __FILE__);
							if (mysql_num_rows($sql) == 0) $pos = 0;
							else {
								$rows = mysql_fetch_array($sql, MYSQL_ASSOC);
								$pos = $rows['pos'] + 1;
							}
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$id.", '".$key."', 'text', '".$val."', ".$pos.")", __LINE__, __FILE__);
						}
					}
				}
			}
		}
		return 1;
	}
	
	function del_client_from_all_group($client_id){
		if (isset($client_id) && (strlen($client_id) > 0)){
			$client_id = intval($client_id);
			$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `client_id` = ".$client_id, __LINE__, __FILE__);		
		}
	}
	
	function add_client_to_group($client_id, $group_id){
		if (isset($client_id) && (strlen($client_id) > 0)){
			$client_id = intval($client_id);
			if (!isset($group_id) || (strlen($group_id) <= 0) || (intval($group_id)==0)) $group_id = 0;
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `id`=".$group_id, __LINE__, __FILE__);
			if (mysql_num_rows($sql) == 1){
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `group_id`=".$group_id." AND `client_id`=".$client_id, __LINE__, __FILE__);
				if (mysql_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_depend` (`id`,`group_id`,`client_id`) VALUES ('', ".$group_id.", ".$client_id.")", __LINE__, __FILE__);
			}
		}
	}
	
	function add_new_registation($login_data, $data, $nopass=0, $form_id){
		global $treeclass;
		$form_groups = array();
		$form_group = $treeclass->get_item_content($form_id);
		if (!isset($login_data['group'])) $login_data['group'] = array();
		if (isset($form_group['block_client_form_groups']['value'])) $form_groups = explode(";", $form_group['block_client_form_groups']['value']);
		unset($form_group);
		if ((isset($login_data['email'])) && (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $login_data['email']))){
			$login_data['email'] = trim($login_data['email']);
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `email`='".$login_data['email']."'", __LINE__, __FILE__);
			$num = mysql_num_rows($sql);
			if ($num > 0) {
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent`='".$row['id']."'", __LINE__, __FILE__);
				$num = mysql_num_rows($sql);
				if ($num > 0) {
					if ((count($data) == 0) && (count($login_data['group'])>0) && (!isset($login_data['login']))){
						$groups = explode(";", $login_data['group']);
						if (is_array($groups)) {
							foreach($groups as $val){
								if ((strlen($val) > 0) && (in_array($val, $form_groups))) {
									$this->add_client_to_group($row['id'], $val);
								}
							}
						}
					} else {
						/*ads by mindaugas komza 2009 09 29 (pridedamas kodas jei keisti vartotoja informacija jam jungiantis pakartotinai)*/
						foreach($data as $val){
							if( $val['value'] != '' ) {
								if( $val['type'] != 'select' ) {
									$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name`='".$val['label']."' AND `parent`='".$row['id']."'", __LINE__, __FILE__);
									$num = mysql_num_rows($sql);
									if($num > 0) {
										$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `name`='".$val['label']."', `type`='".$val['label']."', `value`='".$val['value']."'  WHERE `name`='".$val['label']."' AND `parent`=".$row['id'], __LINE__, __FILE__);
									} else {
										$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$row['id'].", '".$val['label']."', '".$val['label']."', '".$val['value']."', 1)", __LINE__, __FILE__);
									}
								} elseif( $val['type'] == 'select' ) {

								}
							}
						}
						/*END ads by mindaugas komza 2009 09 29 (pridedamas kodas jei keisti vartotoja informacija jam jungiantis pakartotinai)*/
						/*PRIDETI NUOLAIDOS LAUKA PRIE KIEKVIENO VARTOTOJO - KODAS CIA, TIKRINANT JEI LAUKO DAR NERA*/
						/*
						$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."client_data` WHERE type='nuolaida' AND `parent`='".$row['id']."'", __LINE__, __FILE__);
						$num = mysql_num_rows($sql);
						if ($num == 0) {
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$row['id'].", 'nuolaida', 'nuolaida', '0', 1)", __LINE__, __FILE__);
						}
						*/
						/*END PRIDETI NUOLAIDOS LAUKA PRIE KIEKVIENO VARTOTOJO - KODAS CIA, TIKRINANT JEI LAUKO DAR NERA*/
						/*ads by mindaugas komza 2009 09 29 (modifikuotas kliento prisiregistravimas. Jei formoje naudojamas auth_redistribute reiksme, registruojantis nepriloginamas useris)*/
						if(isset($login_data['auth_redistribute']) && ($login_data['auth_redistribute']==1)) {
							$_SESSION['client_login_id'] = $row['id'];
							if(!isset($_SESSION['client_havesess'])){
								$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `sesid` = '".session_id()."'", __LINE__, __FILE__);
								$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `userid` = '".$_SESSION['client_login_id']."'", __LINE__, __FILE__);
								$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."sessions` values('".session_id()."', '".$_SESSION['client_login_id']."', '".$_SERVER['REMOTE_ADDR']."', '".date("YmdHis")."', '".date("YmdHis")."', 'client')", __LINE__, __FILE__);
								$_SESSION['client_havesess'] = 1;
							}
						} else {
					  		return 14;
						}
						/*END ads by mindaugas komza 2009 09 29 (modifikuotas kliento prisiregistravimas. Jei formoje naudojamas auth_redistribute reiksme, registruojantis priloginamas useris)*/
					} 
				}
				$parent = $row['id'];
			} else {
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client` (`id`,`email`,`status`,`regdate`) VALUES ('', '".$login_data['email']."', '000', '".date('YmdHis')."')", __LINE__, __FILE__);
				$parent = mysql_insert_id();
				
				/*ads by mindaugas komza 2009 09 29 (registruojant klienta iterpia ir kitas formos  reiksmes, ne tik email ir psw)*/
				foreach($data as $val){
					if( $val['value'] != '' ) {
						if( $val['type'] != 'select' ) {
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", '".$val['label']."', '".$val['label']."', '".$val['value']."', 1)", __LINE__, __FILE__);
						} elseif( $val['type'] == 'select' ) {
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", '".$val['label']."', '".$val['label']."', '".$val['value'][0]."', 1)", __LINE__, __FILE__);
						}
					}
				}
				/*END ads by mindaugas komza 2009 09 29 (registruojant klienta iterpia ir kitas formos  reiksmes, ne tik email ir psw)*/
				/*PRIDETI NUOLAIDOS LAUKA PRIE KIEKVIENO VARTOTOJO - KODAS CIA, TIKRINANAT JEI LAUKO DAR NERA*/
				//$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", 'nuolaida', 'nuolaida', '0', 1)", __LINE__, __FILE__);
				/*END PRIDETI NUOLAIDOS LAUKA PRIE KIEKVIENO VARTOTOJO - KODAS CIA, TIKRINANAT JEI LAUKO DAR NERA*/
				/*ads by mindaugas komza 2009 09 29 (modifikuotas kliento prisiregistravimas. Jei formoje naudojamas auth_redistribute reiksme, registruojantis priloginamas useris)*/
				if(isset($login_data['auth_redistribute']) && ($login_data['auth_redistribute']==1)) {
					$_SESSION['client_login_id'] = $parent;
					if(!isset($_SESSION['client_havesess'])){
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `sesid` = '".session_id()."'", __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `userid` = '".$_SESSION['client_login_id']."'", __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."sessions` values('".session_id()."', '".$_SESSION['client_login_id']."', '".$_SERVER['REMOTE_ADDR']."', '".date("YmdHis")."', '".date("YmdHis")."', 'client')", __LINE__, __FILE__);
						$_SESSION['client_havesess'] = 1;
					}
				}
				/*END ads by mindaugas komza 2009 09 29 (prilogina klienta be psw)*/

				if (isset($login_data['auth']) && ($login_data['auth']==1)){
					$_SESSION['client_login_id'] = $parent;
					if(!isset($_SESSION['client_havesess'])){
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `sesid` = '".session_id()."'", __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `userid` = '".$_SESSION['client_login_id']."'", __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."sessions` values('".session_id()."', '".$_SESSION['client_login_id']."', '".$_SERVER['REMOTE_ADDR']."', '".date("YmdHis")."', '".date("YmdHis")."', 'client')", __LINE__, __FILE__);
						$_SESSION['client_havesess'] = 1;
					}
				}
				//======= iterpiama shiping informacija
				if (isset($login_data['add_shiping_address']) && ($login_data['add_shiping_address']==1)){
					foreach( $login_data['add_shiping_address_data'] as $key_ship=>$value_ship ) {
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shiping_address` SET `user_id`='".$parent."', `version`=1, `name`='".$key_ship."', `value`='".$value_ship."' ", __LINE__, __FILE__);
					}
				}
			}
			
			if (isset($login_data['group'])) {
				$groups = explode(";", $login_data['group']);
				if (is_array($groups) && is_array($form_groups)) {
					foreach($groups as $val){
						if ((strlen($val) > 0) && ((in_array($val, $form_groups)) || ($nopass!=0))) {
							$this->add_client_to_group($parent, $val);
						} else {
							/*ads by mindaugas komza 2009 09 30 (pasirenkant formoje grupe - vartotjas i ja registrinamas)*/
							if(isset($login_data['auth_redistribute']) && ($login_data['auth_redistribute']==1)) {
								$this->add_client_to_group($parent, $login_data['group']);
							}
							/*END ads by mindaugas komza 2009 09 30 (pasirenkant formoje grupe - vartotjas i ja registrinamas)*/
						}
					}
				}
			}

			if (((isset($login_data['login'])) && (!ereg("[^a-zA-Z0-9_-]", $login_data['login']))) || ($nopass!=0)){
				if ($nopass!=0){
					if (!isset($login_data['login'])) $login_data['login'] = "";
					if (!isset($login_data['pass1'])) $login_data['pass1'] = "";
					if (!isset($login_data['pass2'])) $login_data['pass2'] = "";
				}
				$login_data['login'] = trim($login_data['login']);
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name`='id_login' AND `value`='".$login_data['login']."'", __LINE__, __FILE__);
				if ((mysql_num_rows($sql) > 0) && ($nopass==0)) return 6;
				if (((isset($login_data['pass1'])) && (isset($login_data['pass2'])) && ($login_data['pass1']=$login_data['pass2']))||($nopass!=0)){
					$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", 'id_login', 'id_login', '".$login_data['login']."', 1)", __LINE__, __FILE__);
					$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", 'id_pass', 'id_pass', '".md5($login_data['pass1'])."', 2)", __LINE__, __FILE__);
					foreach($data as $val){
						if (isset($val['value'])){
							if (isset($val['pos'])) $pos = $val['pos'] + 3;
							else $pos = 3;
							if (is_array($val['value']) && (count($val['value'])>0)) {
								if ($val['type'] == 'checkbox'){
									$checkbox_value = "";
									foreach($val['value'] as $key=>$element){
										$key = trim($key);
										$key = addslashes($key);
										if ($element == 'on') $checkbox_value .= $key.";";
									}
								}
								if (isset($checkbox_value) && $checkbox_value != "") $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", '".$val['label']."', '".$val['type']."', '".$checkbox_value."', ".$pos.")", __LINE__, __FILE__);
							} else {
								$val['value'] = trim($val['value']);
								$val['value'] = addslashes($val['value']);
								$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_data` (`id`,`parent`,`name`,`type`,`value`, `pos`) VALUES ('', ".$parent.", '".$val['label']."', '".$val['type']."', '".$val['value']."', ".$pos.")", __LINE__, __FILE__);
							}
						}
					}
				} else return 3;
				/*origin*/
				if (isset($login_data['auth']) && ($login_data['auth']==1)){
					$_SESSION['client_login_id'] = $parent;
					if(!isset($_SESSION['client_havesess'])){
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `sesid` = '".session_id()."'", __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."sessions` WHERE `userid` = '".$_SESSION['client_login_id']."'", __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."sessions` values('".session_id()."', '".$_SESSION['client_login_id']."', '".$_SERVER['REMOTE_ADDR']."', '".date("YmdHis")."', '".date("YmdHis")."', 'client')", __LINE__, __FILE__);
						$_SESSION['client_havesess'] = 1;
					}
				}
				
				if (isset($parent) && ($parent > 0)) $this->send_new_registation_to_admin($parent, $form_id);
				
			} elseif ((isset($login_data['login'])) && (strlen($login_data['login'])>0) && (!ereg("[^a-zA-Z0-9_-]", $login_data['login']))) return 19;
			return 0;
		} else return 13;
		return 0;
	}

	function get_clients_info_by_group_id($group_id){
		$clients = array();
		$group_id = intval($group_id);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `group_id`=".$group_id, __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			$clients[$row['client_id']] = $this->get_clients_info_by_id($row['client_id']);
			$clients[$row['client_id']]['groups'] = $this->get_one_client_groups($row['client_id']);
			if ((count($clients[$row['client_id']]) == 0) || (!isset($clients[$row['client_id']]['main']))) unset($clients[$row['client_id']]);
		}
		return $clients;
	}
	
	function get_clients_info_by_id($id){
		$client = array();
		if (isset($id) && (intval($id) > 0)){
			$id = intval($id);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client` WHERE `id`=".$id, __LINE__, __FILE__);		
			if (mysql_num_rows($sql) == 1){
				$info = mysql_fetch_array($sql, MYSQL_ASSOC);
				$client['main'] = $info;
				$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent`=".$info['id']." ORDER BY `pos` ASC", __LINE__, __FILE__);		
				if (mysql_num_rows($sql2) > 0){
					while ($row = mysql_fetch_array($sql2, MYSQL_ASSOC)) {
						$client['data'][$row['name']]['value'] = stripslashes($row['value']);
						$client['data'][$row['name']]['type'] = $row['type'];
						if( $row['name'] == 'Name' || $row['name'] == 'First name' || $row['name'] == 'name' || $row['name'] == 'Vardas' || $row['name'] == 'vardas' ) {
							$client['main']['login_name'] = $row['value'];
						}
					}
				}
			} 
		}
		$client['groups'] = $this->get_one_client_groups($id);
		return $client;
	}
	
	function get_one_client_groups($id){
		$groups = array();
		if (isset($id) && (strlen($id) > 0)){
			$id = intval($id);
			$sql = $this->mysqlclass->mysql_my_query("SELECT `group_id` FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `client_id`=".$id, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
				$groups[$row['group_id']] = $row['group_id'];
			}
		}
		return $groups;
	}
	
	function clients_del($clients_array){
		foreach($clients_array as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client` WHERE `id` = ".$elem, __LINE__, __FILE__);		
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_data` WHERE `parent` = ".$elem, __LINE__, __FILE__);		
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `client_id` = ".$elem, __LINE__, __FILE__);		
			}
		}
	}
	
	function assign_group_permission($parent, $groups){
		if (isset($parent) && (strlen($parent) > 0)){
			$parent = intval($parent);
			$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_group_depend` WHERE `page_id`=".$parent, __LINE__, __FILE__);		
			foreach($groups as $gr){
				$gr = intval($gr);
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `id`=".$gr, __LINE__, __FILE__);
				if (mysql_num_rows($sql) == 1){
					$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_group_depend` WHERE `group_id`=".$gr." AND `page_id`=".$parent, __LINE__, __FILE__);
					if (mysql_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_group_depend` (`id`,`page_id`,`group_id`, `type`) VALUES ('', ".$parent.", ".$gr.", 'group')", __LINE__, __FILE__);
				}
			}
		}
		return 0;
	}

	function get_page_permission($parent){
		$groups = array();
		if (isset($parent) && (strlen($parent) > 0)){
			$parent = intval($parent);
			$sql = $this->mysqlclass->mysql_my_query("SELECT `group_id` FROM `".$this->setupdata['table_prefix']."client_group_depend` WHERE `page_id`=".$parent, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
				$groups[$row['group_id']] = $row['group_id'];
			}
		}
		return $groups;
	}
	
	function is_client_access_to_page_from_guest($page_id, $client_id){
		global $treeclass;
		$pages = $treeclass->sqlpath($page_id);
		foreach ($pages as $page)
			if ($page['type'] == 'secure_folder01'){
				if ($client_id != NULL) return $this->is_client_access_to_page($page['id'], $client_id);
				else return 0;
			}
		return 1;
	}
	
	function is_client_access_to_page($page_id, $client_id){
		if ($client_id == 0) return 0;
		$page_perm = $this->get_page_permission($page_id);
		$clien_access = $this->get_one_client_groups($client_id);
		$res = array_intersect($page_perm, $clien_access);
		if ((is_array($res)) && (count($res) > 0)) return 1;
		else return 0;
	}

	function delete_from_tree_childs_no_permission($client_id, $tree){
		$new_tree = array();
		$del_mas = array();
		foreach($tree as $key=>$value){
			if ($value['type'] == 'secure_folder01') {
				$access = $this->is_client_access_to_page($value['id'], $client_id);
				if ($access == 1) $new_tree[$key] = $value;
				else $del_mas[$value['id']] = $value;
			} else {
				if (is_array($value['parent'])){
					if (!isset($del_mas[$value['parent']['id']])) $new_tree[$key] = $value;
					else $del_mas[$value['id']] = $value;
				} else $new_tree[$key] = $value;
			}
		}
		return $new_tree;
	}
	
	function user_pass_remind($email, $step, $confirm='0'){
		global $treeclass;
		if (isset($_SESSION['lang'])) $lang = "(`type`='".$_SESSION['lang']."' OR `type`='text')";
		else $lang = "(`type`='".$this->setupdata['default_languages']."' OR `type`='text')";
		
		if ($step=='step1'){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_pass` WHERE ".date("YmdHis")." - `date` > 86400", __LINE__, __FILE__);
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `email` = '".$email."'", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) == 1){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_pass` WHERE `email` = '".$email."'", __LINE__, __FILE__);		
				$id = md5(uniqid(rand(), true));
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."client_pass` (`id`,`email`,`date`) VALUES ('".$id."', '".$email."', '".date('YmdHis')."')", __LINE__, __FILE__);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group`='pass_letter' AND ".$lang, __LINE__, __FILE__);
				while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
					$arr[$row['name']] = stripslashes($row['value']);
				}	
				if (isset($arr['letter_from']) && isset($arr['letter_subject']) && isset($arr['letter_text_before'])){
					$url = $this->setupdata['base_href']."/index.php?module=clients&confirm=".$id."&email=".$email."&act=pass_step2";
					$mail_settings['form_to'] = $email;
					$mail_settings['form_from'] = $arr['letter_from'];
					$mail_settings['form_subject'] = $arr['letter_subject'];
					$output_text = $arr['letter_text_before'];
					$output_text .= "\n".$url;
					$output_html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\"></HEAD><BODY>";
					
					$t = explode("\n\r", $arr['letter_text_before']);
					foreach ($t as $s) {
						if ($s != "") $output_html .= "<DIV>".$s."</DIV>\n";
					}
					$output_html .= "<br><a href='".$url."'>".$url."</a>";
					
					$output_html .= "</BODY></HTML>";
					
					$treeclass->send_form_to_mail($output_html, $output_text, $mail_settings, array());
					return 0;
				} else return 22;
			} else {
				return 21;
			}
		} elseif ($step=='step2'){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."client_pass` WHERE ".date("YmdHis")." - `date` > 86400", __LINE__, __FILE__);			
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `email` = '".$email."'", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) == 1){
				$this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_pass` WHERE `email` = '".$email."' AND `id`='".$confirm."'", __LINE__, __FILE__);
				if (mysql_num_rows($sql) == 1){
					$row = mysql_fetch_array($sql, MYSQL_ASSOC);
					$sql_2 = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."client_data` WHERE `name` = 'id_login' AND `parent` = ".$row['id'], __LINE__, __FILE__);
					$row_2 = mysql_fetch_array($sql_2, MYSQL_ASSOC);
					$new_pass = substr(md5(uniqid(rand(), true)), 5, 8);
					$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='".md5($new_pass)."' WHERE `name`='id_pass' AND `parent`=".$row['id'], __LINE__, __FILE__);
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group`='pass_letter' AND ".$lang, __LINE__, __FILE__);
					while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
						$arr[$row['name']] = stripslashes($row['value']);
					}
					
					if (isset($arr['letter_from']) && isset($arr['letter_subject']) && isset($arr['letter_text_after'])){
						$url = $new_pass;
						$mail_settings['form_to'] = $email;
						$mail_settings['form_from'] = $arr['letter_from'];
						$mail_settings['form_subject'] = $arr['letter_subject'];
						$output_text = $arr['letter_text_after'];
						$output_text .= "\n".$row_2['value']."\n".$url;
						$output_html = $arr['letter_text_after'];
						$output_html .= "<br>".$row_2['value']."<br>".$url;
						$_SERVER['HTTP_REFERER'] = $this->setupdata['base_href']."/index.php";
						$treeclass->send_form_to_mail($output_html, $output_text, $mail_settings, array());
						return 0;
					} else return 22;
				}
			}
		}
	}
	
	function mod_pass_letter_data($from, $subject, $text, $text2, $lang){
		$from = addslashes($from);	
		$subject = addslashes($subject);	
		$text = addslashes($text);	
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group` = 'pass_letter' AND `type`='".$lang."'", __LINE__, __FILE__);		
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'letter_from', '".$from."', '".$lang."', 0, 'pass_letter')", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'letter_subject', '".$subject."', '".$lang."', 0, 'pass_letter')", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'letter_text_before', '".$text."', '".$lang."', 0, 'pass_letter')", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'letter_text_after', '".$text2."', '".$lang."', 0, 'pass_letter')", __LINE__, __FILE__);
	}
	
	function add_fields_to_client($client_id, $form_id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client` WHERE `id` = ".$client_id, __LINE__, __FILE__);		
		if (mysql_num_rows($sql) == 1){
			global $treeclass;
			$form_fields = array();
			$client_fields = array();
			$form_info = $treeclass->get_item_content($form_id);
			$client_info = $this->get_clients_info_by_id($client_id);
			foreach($form_info as $value){
				if (isset($value['type']) && ($value['type'] == 'form')){
					$form_fields = $value['value'];
					break;
				}
			}
			if (isset($client_info['data'])) $client_fields = $client_info['data'];
			$array = array();
			if (is_array($form_fields) && (count($form_fields) > 0)){
				foreach($form_fields as $key=>$value){
					if (!array_key_exists($value['label'], $client_fields)){
						if (isset($key)) {
							$array[base64_encode($value['label'])]['type'] = $value['type'];
							$array[base64_encode($value['label'])]['value'] = "";
						}
					}
				}
			}
			$this->mod_user_registation_data($client_id, $array);
		}
	}
	
	function send_new_registation_to_admin($user_id, $form_id){
		global $smarty, $treeclass;
		$form_info = $treeclass->get_item_content($form_id);
		$user_info = $this->get_clients_info_by_id($user_id);
		if (isset($user_info['data']['id_pass'])) unset($user_info['data']['id_pass']);
		$groups = $this->get_one_client_groups($user_id);
		foreach($groups as $value){
			$user_info['groups'][$value] = $this->group_get_info($value);
		}
		$result = 0;
		if ((isset($form_info['sended_mail_template']['value'])) && (isset($form_info['block_form_subj']['value'])) && (isset($form_info['block_form_to']['value'])) && (strlen($form_info['block_form_to']['value']) > 4) && (strlen($form_info['block_form_subj']['value']) > 0) && (strlen($form_info['sended_mail_template']['value']) > 0)){
			$smarty->assign("user_info", $user_info);
			$tempalate = "guest/mail/".$form_info['sended_mail_template']['value'];
			if ($smarty->template_exists($tempalate)) $output = $smarty->fetch($tempalate);
			$output_text = strip_tags($output);
			$mail_settings['form_to'] = $form_info['block_form_to']['value'];
			$mail_settings['form_from'] = $form_info['block_form_to']['value'];
			foreach($form_info as $key=>$el){
				if (($el['type'] == 'form') && (is_array($el['value'])) && (count($el['value'])>0)){
					foreach($el['value'] as $k=>$v){
						if (isset($v[$k.'_mail01']) && ($v[$k.'_mail01'] == 'on')) {
							if (array_key_exists($v['label'], $user_info['data'])){
								if (!isset($mail_settings['form_to'])) $mail_settings['form_to'] = $user_info['data'][$v['label']]['value'];
								else $mail_settings['form_to'] .= ", ".$user_info['data'][$v['label']]['value'];
							}
						}
					}
				}
			}
			$mail_settings['form_subject'] = $form_info['block_form_subj']['value'];
			$result = $treeclass->send_form_to_mail($output, $output_text, $mail_settings, array());
			$smarty->clear_assign("user_info");
		}
		return $result;
	}
	
	function get_secure_folder_id($array){
		if ($array['type'] == 'secure_folder01'){
			return $array['id'];
		} elseif (is_array($array['parent'])){
			return $this->get_secure_folder_id($array['parent']);
		} else 0;
	}
	
	function emails_export(){
		$result = "";
		$sql = $this->mysqlclass->mysql_my_query("SELECT `email` FROM `".$this->setupdata['table_prefix']."client` ORDER BY `regdate` DESC", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result .= $row['email']."\n";
		header("Content-type: text/plain");
		header('Content-Disposition: attachment; filename="client_emails_'.date('H_i_s').'.csv"');
		header('Content-length: '.strlen($result));
		echo($result);
		exit();
	}
	
	function file_del_with_parent_data($parent, $field_name, $image_id){
		if (!isset($parent) or (intval($parent) < 1)) return NULL;
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."client_data` SET `value`='' WHERE ".$parent." = `parent` AND `name`='".$field_name."'", __LINE__, __FILE__);
	}
}
?>
